﻿using Basic.FlowModel.DB;
using Basic.FlowRemoteModel.NodeFormPower.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowNodeFormPowerDAL : BasicDAL<DBFlowNodeFormPower, long>, IFlowNodeFormPowerDAL
    {
        public FlowNodeFormPowerDAL ( IRepository<DBFlowNodeFormPower> basicDAL ) : base(basicDAL, "node.form.power")
        {
        }

        public void Adds ( long flowId, long nodeId, FormColPower[] powers )
        {
            DBFlowNodeFormPower[] adds = powers.ConvertAll(a => new DBFlowNodeFormPower
            {
                ColId = a.ColId,
                FlowId = flowId,
                NodeId = nodeId,
                Operate = a.Operate,
                Id = IdentityHelper.CreateId()
            });
            this._BasicDAL.Insert(adds);
        }
        public void Set ( long flowId, long nodeId, FormColPower[] powers )
        {
            if ( powers.IsNull() )
            {
                if ( !this._BasicDAL.Delete(a => a.NodeId == nodeId) )
                {
                    throw new ErrorException("flow.form.data.power.delete.fail");
                }
                return;
            }
            DBFlowNodeFormPower[] adds = powers.ConvertAll(a => new DBFlowNodeFormPower
            {
                ColId = a.ColId,
                FlowId = flowId,
                NodeId = nodeId,
                Operate = a.Operate,
                Id = IdentityHelper.CreateId()
            });
            ISqlQueue<DBFlowNodeFormPower> queue = this._BasicDAL.BeginQueue();
            queue.Delete(a => a.FlowId == flowId && a.NodeId == nodeId);
            queue.Insert(adds);
            _ = queue.Submit();
        }
    }
}
